{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dressed-punch",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = [1,2,3,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "elder-nothing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['12', '34'],\n",
       " ['12', '43'],\n",
       " ['13', '24'],\n",
       " ['13', '42'],\n",
       " ['14', '23'],\n",
       " ['14', '32'],\n",
       " ['21', '34'],\n",
       " ['21', '43'],\n",
       " ['23', '14'],\n",
       " ['23', '41'],\n",
       " ['24', '13'],\n",
       " ['24', '31'],\n",
       " ['31', '24'],\n",
       " ['31', '42'],\n",
       " ['32', '14'],\n",
       " ['32', '41'],\n",
       " ['34', '12'],\n",
       " ['34', '21'],\n",
       " ['41', '23'],\n",
       " ['41', '32'],\n",
       " ['42', '13'],\n",
       " ['42', '31'],\n",
       " ['43', '12'],\n",
       " ['43', '21']]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from itertools import permutations\n",
    "\n",
    "l = list(permutations(arr))\n",
    "l = [[str(one[0]) + str(one[1]), str(one[2]) + str(one[3])] for one in l]\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "angry-filename",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['12', '34'],\n",
       " ['12', '43'],\n",
       " ['13', '24'],\n",
       " ['13', '42'],\n",
       " ['14', '23'],\n",
       " ['14', '32'],\n",
       " ['21', '34'],\n",
       " ['21', '43'],\n",
       " ['23', '14'],\n",
       " ['23', '41']]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [one for one in l if 0 <= int(one[0]) <=23 and 0 <= int(one[1]) <= 59]\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "powered-botswana",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_time_length(one):\n",
    "    hour = int(one[0])\n",
    "    minites = int(one[1])\n",
    "    return hour*60 + minites"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "limiting-consistency",
   "metadata": {},
   "outputs": [],
   "source": [
    "l.sort(key=get_time_length, reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bacterial-florida",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['23', '41'],\n",
       " ['23', '14'],\n",
       " ['21', '43'],\n",
       " ['21', '34'],\n",
       " ['14', '32'],\n",
       " ['14', '23'],\n",
       " ['13', '42'],\n",
       " ['13', '24'],\n",
       " ['12', '43'],\n",
       " ['12', '34']]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "adult-bunny",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'23:41'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\":\".join(l[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "absolute-example",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/largest-time-for-given-digits\n",
    "\n",
    "\n",
    "Runtime: 36 ms, faster than 45.47% of Python3 online submissions for Largest Time for Given Digits.\n",
    "Memory Usage: 14.1 MB, less than 84.45% of Python3 online submissions for Largest Time for Given Digits.\n",
    "\n",
    "\n",
    "```python\n",
    "from itertools import permutations\n",
    "\n",
    "\n",
    "def get_time_length(one):\n",
    "    hour = int(one[0])\n",
    "    minites = int(one[1])\n",
    "    return hour*60 + minites\n",
    "\n",
    "class Solution:\n",
    "    def largestTimeFromDigits(self, arr: List[int]) -> str:\n",
    "        #6:02\n",
    "        l = list(permutations(arr))\n",
    "        l = [[str(one[0]) + str(one[1]), str(one[2]) + str(one[3])] for one in l]\n",
    "        l = [one for one in l if 0 <= int(one[0]) <=23 and 0 <= int(one[1]) <= 59]\n",
    "        if len(l):\n",
    "            l.sort(key=get_time_length, reverse=True)\n",
    "            return \":\".join(l[0])\n",
    "        else:\n",
    "            return \"\"\n",
    "        #6:13\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "amended-draft",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
